package com.atguigu.edubanners.controller;


import com.atguigu.commonutils.R;
import com.atguigu.edubanners.entity.FrontBanner;
import com.atguigu.edubanners.service.FrontBannerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author testjava
 * @since 2020-03-07
 */
@Api(description = "后台banner管理接口")
@RestController
@RequestMapping("/edubanners/bannerAdmin")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private FrontBannerService bannerService;

//    //1 分页查询banner
//    @GetMapping("pageBanner/{page}/{limit}")
//    public R pageBanner(@PathVariable long page, @PathVariable long limit) {
//        Page<FrontBanner> pageBanner = new Page<>(page,limit);
//        bannerService.page(pageBanner,null);
//        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
//    }


    //1.获取所有的轮播图
    @ApiOperation("获取所有的轮播图")
    @GetMapping("getAllBanners")
    public R getAllBanners() {
        LambdaQueryWrapper<FrontBanner> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(FrontBanner::getSort);
        List<FrontBanner> allFrontBanner = bannerService.list(wrapper);
        return R.ok().data("getAllBanners", allFrontBanner);
    }

    //2.根据id获取Banner
    @ApiOperation(value = "根据id获取Banner")
    @GetMapping("getBannerById/{id}")
    public R get(@PathVariable String id) {
        FrontBanner banner = bannerService.getById(id);
        return R.ok().data("getBannerById", banner);
    }

    //3.修改Banner
    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody FrontBanner banner) {
        System.out.println(banner);
        System.out.println("=================================]");
        boolean b = bannerService.updateById(banner);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    //3. 添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody FrontBanner banner) {
        bannerService.save(banner);
        return R.ok();
    }


    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }
}

